package com.aaa.dao.impl;

import com.aaa.dao.MenuDao;
import com.aaa.entity.Menu;
import com.aaa.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class MenuDaoImpl implements MenuDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private RowMapper<Menu> rowMapper=new RowMapper<Menu>() {
        public Menu mapRow(ResultSet resultSet, int i) throws SQLException {
            Menu menu=new Menu();
            menu.setId(resultSet.getInt("id"));
            menu.setName(resultSet.getString("name"));
            menu.setPath(resultSet.getString("path"));
            menu.setPid(resultSet.getInt("pid"));
            menu.setLev(resultSet.getInt("lev"));
            menu.setNamespace(resultSet.getString("namespace"));
            return menu;

        }
    };

    public List<Menu> myListMenu(User user) {
        String sql="select distinct m.* from user u inner join user_role ur on u.id=ur.u_id\n" +
                "\t\t\t\t\t\t\t\t\t inner join role r on ur.r_id=r.id\n" +
                "\t\t\t\t\t\t\t\t\t inner join role_menu rm on rm.r_id=r.id\n" +
                "\t\t\t\t\t\t\t\t\t inner join menu m on m.id=rm.m_id\n" +
                "\t\t\t\t\t\t\t\t\t where u.id=?";
        List<Menu> result = jdbcTemplate.query(sql, rowMapper, user.getId());
        return result;
    }

    public List<Menu> myOneMenu(User user) {
        String sql="select distinct m.* from user u inner join user_role ur on u.id=ur.u_id\n" +
                "\t\t\t\t\t\t\t\t\t inner join role r on ur.r_id=r.id\n" +
                "\t\t\t\t\t\t\t\t\t inner join role_menu rm on rm.r_id=r.id\n" +
                "\t\t\t\t\t\t\t\t\t inner join menu m on m.id=rm.m_id\n" +
                "\t\t\t\t\t\t\t\t\t where u.id=? and m.lev=1";
        List<Menu> result = jdbcTemplate.query(sql, rowMapper, user.getId());
        return result;
    }

    public List<Menu> myTwoMenu(User user,Integer pid) {
        String sql = "select distinct m.* from user u inner join user_role ur on u.id=ur.u_id\n" +
                "\t\t\t\t\t\t\t\t\t inner join role r on ur.r_id=r.id\n" +
                "\t\t\t\t\t\t\t\t\t inner join role_menu rm on rm.r_id=r.id\n" +
                "\t\t\t\t\t\t\t\t\t inner join menu m on m.id=rm.m_id\n" +
                "\t\t\t\t\t\t\t\t\t where u.id=? and m.lev=2 and m.pid=?";
        List<Menu> result = jdbcTemplate.query(sql, rowMapper, user.getId(), pid);
        return result;
    }


        /**
         * 查询所有的一级菜单
         * @return
         */
        public List<Menu> listOneMenus(){
            String sql = "select distinct m.* from user u inner join  user_role ur on u.id=ur.u_id\n" +
                    "                       inner join  role r on r.id=ur.r_id\n" +
                    "                       inner join  role_menu rm on r.id=rm.r_id\n" +
                    "                       inner join  menu m on m.id=rm.m_id\n" +
                    "                       where  m.lev=1";
            List<Menu> result= this.jdbcTemplate.query(sql, rowMapper);
            return result;


        }


    /**
     * 查询角色能操作的所有菜单
     * @param rid
     * @return
     */
    public List<Menu> listRoleMenus(Long rid) {
        String sql = "select m.* from role_menu rm inner join menu m on rm.m_id=m.id where rm.r_id=?";
        List<Menu> result= this.jdbcTemplate.query(sql, rowMapper,rid);
        return result;
    }

    /**
     * 查询某个一级菜单下的二级菜单
     * @return
     */
    public List<Menu> listTwoMenus(Integer pid){
        String sql = "select distinct m.* from user u inner join  user_role ur on u.id=ur.u_id\n" +
                "                       inner join  role r on r.id=ur.r_id\n" +
                "                       inner join  role_menu rm on r.id=rm.r_id\n" +
                "                       inner join  menu m on m.id=rm.m_id\n" +
                "                       where m.lev=2 and m.pid=?";
        List<Menu> result= this.jdbcTemplate.query(sql, rowMapper,pid);
        return result;


    }

}
